﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StoreSQLSugar.UserControls
{
    public partial class UCPager : UserControl
    {
        public event EventHandler PageChanged;
        public UCPager()
        {
            InitializeComponent();
        }

        private void UCPager_Load(object sender, EventArgs e)
        {
            cbbPageSize.SelectedIndexChanged -= cbbPageSize_SelectedIndexChanged;
            cbbPageSize.SelectedIndex = 0;
            cbbPageSize.SelectedIndexChanged += cbbPageSize_SelectedIndexChanged;
        }

        #region 公开属性
        private int page = 1;
        /// <summary>
        /// 当前页码，默认值1
        /// </summary>
        public int Page
        {
            get { return page; }
            set
            {
                page = value;
                txtPage.Text = page.ToString();
                PageChanged?.Invoke(this, EventArgs.Empty);
                DisabledButton(page, totalPage);
                Refresh();
            }
        }

        private int pageSize = 10;
        /// <summary>
        /// 每页显示的条数，默认值：1
        /// </summary>
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; Refresh(); }
        }

        private int totalPage = 0;
        /// <summary>
        /// 总页码，默认值0
        /// </summary>
        public int TotalPage
        {
            get { return totalPage; }
            set
            {
                totalPage = value;
                lblPageAndTotalPage.Text = $"{Page}/{totalPage}";
                Refresh();
            }
        }
        #endregion

        #region 方法
        private void DisabledButton(int page, int totalPage)
        {
            if (page > 1 && page < totalPage)
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                btnNext.Enabled = true;
                btnLast.Enabled = true;
            }
            else
            {
                if (page <= 1)
                {
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                }
                else
                {
                    btnFirst.Enabled = true;
                    btnPrev.Enabled = true;
                }

                if (page >= totalPage)
                {
                    btnNext.Enabled = false;
                    btnLast.Enabled = false;
                }
                else
                {
                    btnNext.Enabled = true;
                    btnLast.Enabled = true;
                }
            }
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            Page = 1;
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (Page > 1) Page--;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (Page < TotalPage) Page++;
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            Page = TotalPage;
        }

        private void cbbPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox ps = (ComboBox)sender;
            PageSize = int.Parse(ps.SelectedItem.ToString());
            Page = 1;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtPage.Text))
            {
                MessageBox.Show("请输入页码！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!int.TryParse(txtPage.Text, out int p)) return;
            if (p > totalPage)
            {
                MessageBox.Show($"输入的页码不正确，页码范围[1,{totalPage}]！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Page = p;
        }

        private void txtPage_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 只允许数字
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        #endregion
    }
}
